package com.loveprogrammer.lowercode.dna.party.dna;

import com.loveprogrammer.lowercode.bo.PartyAccountBo;
import com.loveprogrammer.lowercode.bo.PartyBO;
import com.loveprogrammer.lowercode.dna.common.CodeDefConst;
import com.loveprogrammer.lowercode.dna.common.CommonInfo;
import com.loveprogrammer.lowercode.dna.instance.bo.Cell;
import com.loveprogrammer.lowercode.dna.instance.bo.Inst;
import com.loveprogrammer.lowercode.dna.service.InstService;
import com.loveprogrammer.lowercode.dna.service.impl.InstServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Console;

/**
 * @ClassName PartyHelper
 * @Description TODO
 * @Author admin
 * @Date 2024/6/19 17:47
 * @Version 1.0
 */
@Service
public class PartyHelper {

    @Autowired
    private InstService instService;

    public PartyHelper(InstService instService) {
        this.instService = instService;
    }

    // 将PartyBO对象转换成Inst对象
    public Inst partyBO2Inst(PartyBO party) {
        Inst partyInst = instService.initInst(CommonInfo.getDefault(),
                CodeDefConst.BUSINESS_TYPE_PARTY, CodeDefConst.DNA_CODE_PARTY);

        Cell partyCell = partyInst.getSingleCell();
        partyCell.setId(party.getId());
        partyCell.setVaByName("partyname",party.getPartyName());
        partyCell.setVaByName("certType",party.getCertType());
        partyCell.setVaByName("certId",party.getCertId());
        partyCell.setVaByName("contactMobileNo",party.getContactMobileNo());
        partyCell.setVaByName("contact",party.getContact());


        Inst acountInst = instService.initInst(CommonInfo.getDefault(),
                CodeDefConst.BUSINESS_TYPE_PARTY, CodeDefConst.DNA_CODE_PARTY_ACCOUNT);

        acountInst.getCells().clear();

        for (PartyAccountBo account : party.getAccounts()) {
            Inst tempAcountInst = instService.initInst(CommonInfo.getDefault(), CodeDefConst.BUSINESS_TYPE_PARTY, CodeDefConst.DNA_CODE_PARTY_ACCOUNT);
            Cell singleCell = tempAcountInst.getSingleCell();
            singleCell.setId(account.getId());
            singleCell.setVaByName("accountName",account.getAccountName());
            singleCell.setVaByName("accountNo",account.getAccountNo());
            singleCell.setVaByName("bankName",account.getBankName());
            acountInst.addCell(singleCell);
        }
        partyCell.addChildInst(acountInst);
        return partyInst;
    }

    public PartyBO inst2PartyBo( Inst partyInst) {
        PartyBO party = new PartyBO();
        Cell partyCell = partyInst.getSingleCell();

        party.setId(partyCell.getId());
        party.setPartyName(partyCell.getRawStringByName("partyName"));
        party.setCertType(partyCell.getRawStringByName("certType"));
        party.setCertId(partyCell.getRawStringByName("certId"));
        party.setContactMobileNo(partyCell.getRawStringByName("contactNo"));
        party.setContact(partyCell.getRawStringByName("contact"));

        Inst accountInst = partyCell.getChildInst(CodeDefConst.DNA_NAME_PARTY);

        if(accountInst != null && accountInst.getCells().size() > 0) {
            for (Cell accountInstCell : accountInst.getCells()) {
                PartyAccountBo account = new PartyAccountBo();
                account.setId(accountInstCell.getId());
                account.setAccountName(accountInstCell.getRawStringByName("accountName"));
                account.setBankName(accountInstCell.getRawStringByName("bankName"));
                account.setAccountNo(accountInstCell.getRawStringByName("accountNo"));
                party.getAccounts().add(account);
            }
        }
        return party;
    }

    public static void main(String[] args) {
        PartyHelper helper = new PartyHelper(new InstServiceImpl());

    }
}
